#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <linux/input.h>

#define KEY0VALUE          0XF0
#define INVAKEY            0X00

static struct input_event inputevent;

int main(int argc, char *argv[])
{
	int ret;
	int fd, retvalue;
	unsigned char cnt = 0;
	char *filename;
	unsigned char databuf;
	
	if(argc != 2){
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	/* 打开led驱动 */
	fd = open(filename, O_RDWR);
	if(fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	while(1){
		ret = read(fd,&inputevent,sizeof(inputevent));
		printf("ret %d \r\n", ret);
		if(ret >0){
			switch(inputevent.type){
				case EV_KEY:
				if(inputevent.code < BTN_MISC){
						printf("key %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
				} else {
						printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
				}
				break;

				/* 其他类型的事件，自行处理 */
				case EV_REL:
					printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					break;
				case EV_ABS:
					printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					break;
				case EV_MSC:
					printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					break;
				case EV_SW:
					printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					break;
			}
		}
		else{
			printf("i can't read the data!\r\n");
		}
	}
	retvalue = close(fd); /* 关闭文件 */
	if(retvalue < 0){
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}